package com.cloud.mkz.core.mvc.util;

import cn.hutool.core.util.StrUtil;
import com.cloud.mkz.admin.api.dto.AuthedUser;
import com.cloud.mkz.admin.api.entity.RbacUser;
import com.cloud.mkz.core.common.constant.CacheConstants;
import com.cloud.mkz.core.common.constant.SecurityConstants;
import com.cloud.mkz.core.common.exception.SsoException;
import com.cloud.mkz.core.common.utils.RedisUtil;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@Slf4j
@UtilityClass
public class InterceptorUtil {

	/**
	 * 验证SSO的Token信息
	 * @param request
	 * @param requestUrl
	 * @return UserBean
	 */
	public static AuthedUser checkToken(HttpServletRequest request, String requestUrl, RedisUtil redisUtil) {
		String accessToken = request.getHeader("X-AUTH-TOKEN");
		if(StrUtil.isEmpty(accessToken)) {
			throw new SsoException(requestUrl, "noToken", "登录Token丢失");
		}
		if(!redisUtil.hasKey(CacheConstants.PROJECT_OAUTH_TOKEN + accessToken)) {
			throw new SsoException(requestUrl, "tokenTimeOut", "登录Token已失效");
		}
		AuthedUser authedUser = (AuthedUser)redisUtil.get(CacheConstants.PROJECT_OAUTH_TOKEN + accessToken);
		if(null==authedUser || null==authedUser.getUserId()) {
			throw new SsoException(requestUrl, "userNotExist", "用户不存在");
		}
//		//将redis中 用户信息 放到session中
//		Object loginRedisObject = redisUtil.get("LoginRedisBean"+userBean.getId());
//		if(null==loginRedisObject||StrUtil.isEmpty(loginRedisObject.toString())) {
//			throw new SsoException(requestUrl, "sessionTimeOut", "会话超时请重新登录");
//		}
//		LoginRedisBean loginRedisBean = JSONObject.toJavaObject(JSONObject.parseObject(loginRedisObject.toString()), LoginRedisBean.class);
//		if(null==loginRedisBean||null==loginRedisBean.getUSERINFO()) {
//			throw new SsoException(requestUrl, "sessionTimeOut", "会话超时请重新登录");
//		}

		HttpSession session = ((HttpServletRequest) request).getSession();
//		session.setAttribute(Constants.USERINFO, loginRedisBean.getUSERINFO());
//		session.setAttribute(Constants.DWDJ, loginRedisBean.getDWDJ());
//		session.setAttribute(Constants.OPERROLEVIEW, loginRedisBean.getOPERROLEVIEW());
//		session.setAttribute(Constants.MYCATALOG, loginRedisBean.getMYCATALOG());
//		session.setAttribute(Constants.MYMENUURL, loginRedisBean.getMYMENUURL());
//		session.setAttribute(Constants.MYCLIENTMENUURL, loginRedisBean.getMYCLIENTMENUURL());
		session.setAttribute(SecurityConstants.SESSION_X_OUTH_TOKEN, accessToken);
		//如果没重新登录，设置2小时过期
		saveRedis(CacheConstants.PROJECT_OAUTH_TOKEN + accessToken, authedUser, 2, redisUtil);
//		PermissionChecker.checkPermission(request, requestUrl, redisUtil);
		return authedUser;
	}
	/**
	  * 延长登录缓存的时间
	 * @param key
	 * @param value
	 * @param expire
	 * @param redisUtil
	 */
	public static boolean saveRedis(String key, Object value, long expire, RedisUtil redisUtil) {
	    boolean keyExist = redisUtil.hasKey(key);
	    // NX是不存在时才set， XX是存在时才set， EX是秒，PX是毫秒
	    if (keyExist) {
	    	redisUtil.del(key);
	    }
	    return redisUtil.set(key, value, expire, TimeUnit.HOURS);
	}
}
